﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace WcfContrib.Hosting
{
    /// <summary>
    /// Represents a factory for creating a service host of type WcfContrib.Hosting.ServiceHost
    /// </summary>
    public class ServiceHostFactory : System.ServiceModel.Activation.ServiceHostFactory
    {
        /// <summary>
        /// Creates a <see cref="T:System.ServiceModel.ServiceHost"/> for a specified type of service with a specific base address.
        /// </summary>
        /// <param name="serviceType">Specifies the type of the service.</param>
        /// <param name="baseAddresses">The <see cref="T:System.Array"/> of type <see cref="T:System.Uri"/> that contains the base addresses for the service hosted.</param>
        /// <returns>
        /// A <see cref="T:System.ServiceModel.ServiceHost"/> for the type of service specified with a specific base address.
        /// </returns>
        protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            Type genericHost = typeof(ServiceHost<>);

            Type hostType = genericHost.MakeGenericType(serviceType);

            System.ServiceModel.ServiceHost host = (System.ServiceModel.ServiceHost)Activator.CreateInstance(hostType, baseAddresses);

            return host;
        }
    }
}
